package ueg.org.trabalho.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class Dao {

    private static Configuration configuration;
    private static ServiceRegistry serviceRegistry;
    private static SessionFactory sessionFactory;

    @SuppressWarnings("deprecation")
    public Dao() {
        if (configuration == null) {
            configuration = new Configuration();
            configuration.configure();
        }

        if (serviceRegistry == null) {
            serviceRegistry = new ServiceRegistryBuilder().applySettings(
                    configuration.getProperties()).build();
        }

        if (sessionFactory == null) {
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        }
    }

    protected Session getSession() {
        return sessionFactory.openSession();
    }
}